import Vue from 'vue'
import Router from 'vue-router'
import store from './../store'

import Home from '@/views/Home'
import order from '@/views/order/order'
import orderDeal from '@/views/order/orderDeal'
import newOrder from '@/views/order/orderAdd'
import orderAdd from '@/views/order/orderAdd'
import loanApply from '@/views/loan/loanApply'
import loanApplyDeal from '@/views/loan/loanApplyDeal'
import loanApplyException from '@/views/loan/loanApplyException'
import playMonenApply from '@/views/order/playMonenApply'
import syntheticalInput from '@/views/order/syntheticalInputList'
import synInput from '@/views/order/synInput'
import playMonenConfirm from '@/views/order/playMonenConfirm'
import playMonenConfirmDeal from '@/views/order/playMonenConfirmDeal'
//征信
import creditEnter from '@/views/credit/creditEnter'
import creditList from '@/views/credit/creditList'
//审核
import check from '@/views/check/check'
import checkModify from '@/views/check/checkModify'
import checkException from '@/views/check/checkException'
import checkLoan from '@/views/check/checkLoan'
import checkLoanManager from '@/views/check/checkLoanManager'
import checkExceptionDetail from '@/views/check/checkExceptionDetail'
//财务
import financeNotificationList from '@/views/finance/notificationList'
import financeVerify from '@/views/finance/financeVerify'
import GPSConfirm from '@/views/finance/GPSConfirm'
import GPSConfirmDeal from '@/views/finance/GPSConfirmDeal'
import financeAccount from '@/views/finance/financeAccount'
import financeAccountDetails from '@/views/finance/financeAccountDetails'

import message from '@/components/messagelist'
//资产保全
import OverdueList from '@/components/AssetProtection/OverdueList'
import UrgeManageTab from '@/components/AssetProtection/UrgeManageTab'
import urgeTask from '@/components/AssetProtection/urgeTask'
import GuaranteeManageList from '@/components/AssetProtection/GuaranteeManageList'
import GuaranteeManageDetail from '@/components/AssetProtection/GuaranteeManageDetail'

// 懒加载方式，当路由被访问的时候才加载对应组件
const Login = resolve => require(['@/views/Login'], resolve)

Vue.use(Router)

let router = new Router({
    routes: [{
            path: '/login',
            name: '登录',
            component: Login
        },
        {
            path: '/',
            component: Home,
            redirect: '/order',
            leaf: true,
            level: 1,
            name: 'order',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/order', component: order, name: '我的订单' },
                { path: '/orderDeal', component: loanApplyDeal, name: '订单处理' }
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'newOrder',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/newOrder', component: newOrder, name: '新建订单' }
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'orderAdd',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/orderAdd', component: orderAdd, name: '新建订单new' }
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'loanApply',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/loanApply', component: loanApply, name: '贷款申请' },
                { path: '/loanApplyDeal', component: loanApplyDeal, name: '贷款申请详情' },
                { path: '/loanApplyException', component: loanApplyException, name: '通融申请' }
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'syntheticalInput',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/syntheticalInput', component: syntheticalInput, name: '综合处理' },
                { path: '/synInput', component: synInput, name: '综合录入' }
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'playMonenApply',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/playMonenApply', component: playMonenApply, name: '打款申请' }
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'playMonenConfirm',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/playMonenConfirm', component: playMonenConfirm, name: '打款审核' },
                { path: '/playMonenConfirmDeal', component: playMonenConfirmDeal, name: '打款确认详情' }
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'creditList',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/creditList', component: creditList, name: '征信列表' },
                { path: '/creditEnter', component: creditEnter, name: '征信结果录入' }
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'OverdueList',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/AssetProtection/OverdueList', component: OverdueList, name: '逾期清单' },
                { path: '/AssetProtection/UrgeManageTab', component: UrgeManageTab, name: '催收管理' }
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'urgeTask',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/AssetProtection/urgeTask', component: urgeTask, name: '催收管理' },
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'GuaranteeManageList',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/AssetProtection/GuaranteeManageList', component: GuaranteeManageList, name: '担保管理' },
                { path: '/AssetProtection/GuaranteeManageDetail', component: GuaranteeManageDetail, name: '担保管理' },
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'financeNotificationList',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/financeNotificationList', component: financeNotificationList, name: '打款通知' },
                { path: '/financeVerify', component: financeVerify, name: '打款确认' }
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'check',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/check', component: check, name: store.state.power == 'ROLE_COOPERATIVE' ? '修改审核' : '我的审核' },
                { path: '/checkModify', component: checkModify, name: '修改审批' },
                { path: '/checkException', component: checkException, name: '通融审批' },
                { path: '/checkExceptionDetail', component: checkExceptionDetail, name: '通融审批' },
                { path: '/checkLoan', component: checkLoan, name: '贷款审批' },
                { path: '/checkLoanManager', component: checkLoanManager, name: '贷款审批' }
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'GPSConfirm',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/GPSConfirm', component: GPSConfirm, name: 'GPS确认' },
                { path: '/GPSConfirmDeal', component: GPSConfirmDeal, name: 'GPS确认详情' }
            ]
        },
        {
            path: '/',
            component: Home,
            leaf: true,
            level: 1,
            name: 'financeAccount',
            menuShow: false,
            iconCls: 'el-icon-menu',
            children: [
                { path: '/financeAccount', component: financeAccount, name: '订单结算' },
                { path: '/financeAccountDetails', component: financeAccountDetails, name: '订单结算详情' }
            ]
        },
        {
            path: '/',
            name: 'home',
            component: Home,
            menuShow: false,
            leaf: true, // 只有一个节点
            children: [
                { path: '/messagelist', component: message, name: '消息列表' }
            ]
        }
    ]

})

router.beforeEach((to, from, next) => {
    if (to.path.startsWith('/login')) {
        window.sessionStorage.removeItem('access-user')
        next()
    } else {
        let user = JSON.parse(window.sessionStorage.getItem('access-user'))
        if (!user) {
            next({ path: '/login' })
        } else {
            next()
        }
    }
})

export default router
